package cn.edu.scau.cmi.ema.dao;

import java.util.Arrays;
import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.Set;

import javax.persistence.EntityManager;
import javax.persistence.NoResultException;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import org.springframework.dao.DataAccessException;

import org.springframework.stereotype.Repository;

import org.springframework.transaction.annotation.Transactional;

import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.Session;


import cn.edu.scau.cmi.ema.domain.*;
import cn.edu.scau.cmi.ema.domain.base.CmiPagination;


import cn.edu.scau.cmi.ema.util.CmiSqlUtil;

import java.util.Calendar;

@Repository("CrawlerknowledgeDAO")
@Transactional
public class CrawlerknowledgeDAOImpl extends AbstractJpaDao<Crawlerknowledge> implements CrawlerknowledgeDAO {

	private final static Set<Class<?>> dataTypes = new HashSet<Class<?>>(
			Arrays.asList(new Class<?>[] { Crawlerknowledge.class }));

	@PersistenceContext(unitName = "ema")
	private EntityManager entityManager;

	public CrawlerknowledgeDAOImpl() {
		super();
	}

	public EntityManager getEntityManager() {
		return entityManager;
	}

	public Set<Class<?>> getTypes() {
		return dataTypes;
	}

	@Transactional
	public Crawlerknowledge findCrawlerknowledgeById(Integer id) throws DataAccessException {

		return findCrawlerknowledgeById(id, -1, -1);
	}

	@Transactional
	public Crawlerknowledge findCrawlerknowledgeById(Integer id, int startResult, int maxRows)
			throws DataAccessException {
		try {
			Query query = createNamedQuery("findCrawlerknowledgeById", startResult, maxRows, id);
			return (Crawlerknowledge) query.getSingleResult();
		} catch (NoResultException nre) {
			return null;
		}
	}

	@Transactional
	public Set<Crawlerknowledge> findCrawlerknowledgeByName(String name) throws DataAccessException {

		return findCrawlerknowledgeByName(name, -1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Crawlerknowledge> findCrawlerknowledgeByName(String name, int startResult, int maxRows)
			throws DataAccessException {
		Query query = createNamedQuery("findCrawlerknowledgeByName", startResult, maxRows, name);
		return new LinkedHashSet<Crawlerknowledge>(query.getResultList());
	}

	@Transactional
	public Set<Crawlerknowledge> findCrawlerknowledgeByNameContaining(String name) throws DataAccessException {

		return findCrawlerknowledgeByNameContaining(name, -1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Crawlerknowledge> findCrawlerknowledgeByNameContaining(String name, int startResult, int maxRows)
			throws DataAccessException {
		Query query = createNamedQuery("findCrawlerknowledgeByNameContaining", startResult, maxRows, name);
		return new LinkedHashSet<Crawlerknowledge>(query.getResultList());
	}

	@Transactional
	public Set<Crawlerknowledge> findCrawlerknowledgeByCrawltime(Calendar crawltime) throws DataAccessException {

		return findCrawlerknowledgeByCrawltime(crawltime, -1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Crawlerknowledge> findCrawlerknowledgeByCrawltime(Calendar crawltime, int startResult, int maxRows)
			throws DataAccessException {
		Query query = createNamedQuery("findCrawlerknowledgeByCrawltime", startResult, maxRows, crawltime);
		return new LinkedHashSet<Crawlerknowledge>(query.getResultList());
	}

	@Transactional
	public Set<Crawlerknowledge> findCrawlerknowledgeByExist(Boolean exist) throws DataAccessException {

		return findCrawlerknowledgeByExist(exist, -1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Crawlerknowledge> findCrawlerknowledgeByExist(Boolean exist, int startResult, int maxRows)
			throws DataAccessException {
		Query query = createNamedQuery("findCrawlerknowledgeByExist", startResult, maxRows, exist);
		return new LinkedHashSet<Crawlerknowledge>(query.getResultList());
	}

	@Transactional
	public Set<Crawlerknowledge> findCrawlerknowledgeByTitlerownum(Integer titlerownum) throws DataAccessException {

		return findCrawlerknowledgeByTitlerownum(titlerownum, -1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Crawlerknowledge> findCrawlerknowledgeByTitlerownum(Integer titlerownum, int startResult, int maxRows)
			throws DataAccessException {
		Query query = createNamedQuery("findCrawlerknowledgeByTitlerownum", startResult, maxRows, titlerownum);
		return new LinkedHashSet<Crawlerknowledge>(query.getResultList());
	}

	@Transactional
	public Set<Crawlerknowledge> findCrawlerknowledgeByHash(Integer hash) throws DataAccessException {

		return findCrawlerknowledgeByHash(hash, -1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Crawlerknowledge> findCrawlerknowledgeByHash(Integer hash, int startResult, int maxRows)
			throws DataAccessException {
		Query query = createNamedQuery("findCrawlerknowledgeByHash", startResult, maxRows, hash);
		return new LinkedHashSet<Crawlerknowledge>(query.getResultList());
	}

	@Transactional
	public Set<Crawlerknowledge> findCrawlerknowledgeByCrawlerfilestatus(Integer crawlerfilestatus)
			throws DataAccessException {

		return findCrawlerknowledgeByCrawlerfilestatus(crawlerfilestatus, -1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Crawlerknowledge> findCrawlerknowledgeByCrawlerfilestatus(Integer crawlerfilestatus, int startResult,
			int maxRows) throws DataAccessException {
		Query query = createNamedQuery("findCrawlerknowledgeByCrawlerfilestatus", startResult, maxRows,
				crawlerfilestatus);
		return new LinkedHashSet<Crawlerknowledge>(query.getResultList());
	}

	@Transactional
	public Set<Crawlerknowledge> findCrawlerknowledgeByDescription(String description) throws DataAccessException {

		return findCrawlerknowledgeByDescription(description, -1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Crawlerknowledge> findCrawlerknowledgeByDescription(String description, int startResult, int maxRows)
			throws DataAccessException {
		Query query = createNamedQuery("findCrawlerknowledgeByDescription", startResult, maxRows, description);
		return new LinkedHashSet<Crawlerknowledge>(query.getResultList());
	}

	@Transactional
	public Set<Crawlerknowledge> findCrawlerknowledgeByDescriptionContaining(String description)
			throws DataAccessException {

		return findCrawlerknowledgeByDescriptionContaining(description, -1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Crawlerknowledge> findCrawlerknowledgeByDescriptionContaining(String description, int startResult,
			int maxRows) throws DataAccessException {
		Query query = createNamedQuery("findCrawlerknowledgeByDescriptionContaining", startResult, maxRows,
				description);
		return new LinkedHashSet<Crawlerknowledge>(query.getResultList());
	}

	@Transactional
	public Crawlerknowledge findCrawlerknowledgeByPrimaryKey(Integer id) throws DataAccessException {
		return findCrawlerknowledgeByPrimaryKey(id, -1, -1);
	}

	@Transactional
	public Crawlerknowledge findCrawlerknowledgeByPrimaryKey(Integer id, int startResult, int maxRows)
			throws DataAccessException {
		try {
			Query query = createNamedQuery("findCrawlerknowledgeByPrimaryKey", startResult, maxRows, id);
			return (Crawlerknowledge) query.getSingleResult();
		} catch (NoResultException nre) {
			return null;
		}
	}

	@Transactional
	public Set<Crawlerknowledge> findCrawlerknowledgesByIds(Set<Integer> ids) throws DataAccessException {
		try {
			Query query = createNamedQuery("findCrawlerknowledgesByIds", -1, -1, ids);
			return new HashSet(query.getResultList());
		} catch (NoResultException nre) {
			return null;
		}
	}

	@Transactional
	public Set<Crawlerknowledge> findAllCrawlerknowledges() throws DataAccessException {

		return findAllCrawlerknowledges(-1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Crawlerknowledge> findAllCrawlerknowledges(int startResult, int maxRows) throws DataAccessException {
		Query query = createNamedQuery("findAllCrawlerknowledges", startResult, maxRows);
		return new LinkedHashSet<Crawlerknowledge>(query.getResultList());
	}

	@SuppressWarnings("unchecked")
	public Set<Crawlerknowledge> findCrawlerknowledgesByUrl(Integer urlId, int start, int max)
			throws DataAccessException {
		Query query = createNamedQuery("findAllCrawlerknowledgesByUrl", start, max, urlId);
		return new LinkedHashSet<Crawlerknowledge>(query.getResultList());
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Crawlerknowledge> findCrawlerknowledgeByCompositeProperities(String sql, int startResult, int maxRows) {
		return new LinkedHashSet<Crawlerknowledge>(createQuery(sql, startResult, maxRows).getResultList());
	}

	@Transactional
	public Set<Crawlerknowledge> findCrawlerknowledgesByDetachedCriteria(CmiPagination page)
			throws DataAccessException {
		DetachedCriteria detachedCriteria = CmiSqlUtil.createDetachedCriteria(page, Crawlerknowledge.class);
		Session session = entityManager.unwrap(Session.class);
		Set<Crawlerknowledge> crawlerknowledges = new HashSet(detachedCriteria.getExecutableCriteria(session)
				.setFirstResult(page.getSearchBeginCursor()).setMaxResults(page.getPaginationSize()).list());

		return crawlerknowledges;
	}

	@Transactional
	public Crawlerknowledge findCrawlerknowledgeByPhysicalPrimaryKey(Integer urlId) throws DataAccessException {
		return findCrawlerknowledgeByPhysicalPrimaryKey(urlId, -1, -1);
	}

	@Transactional
	public Crawlerknowledge findCrawlerknowledgeByPhysicalPrimaryKey(Integer urlId, int startResult, int maxRows)
			throws DataAccessException {
		try {
			Query query = createNamedQuery("findCrawlerknowledgeByPhyscialPrimaryKey", startResult, maxRows, urlId);
			return (Crawlerknowledge) query.getSingleResult();
		} catch (NoResultException nre) {
			return null;
		}
	}

	public boolean canBeMerged(Crawlerknowledge entity) {
		return true;
	}
}
